package com.hnust.aopdemo.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.omg.CORBA.Object;
import org.springframework.stereotype.Component;

/**
 * @Before("matchAnno()")
 * @After("matchAnno())") //相当于finally
 * @AfterReturning("matchException()")
 * @AfterThrowing("matchException()")
 * @Around("matchException()")
 * @Before(value = "matchLongArg() && args(productId)")
 * public void beforeWithArgs(Long productId)
 * @AfterReturning(value = "matchReturn()",returning = "returnValue")
 * public void getReulst(Object returnValue)
 * Created by cat on 2017-02-19.
 */
@Aspect
@Component
public class AdviceAspectConfig {

    /******pointcut********/

    @Pointcut("@annotation(com.hnust.aopdemo.security.AdminOnly) && within(com.hnust.aopdemo..*)")
    public void matchAnno(){}

    @Pointcut("execution(* *..find*(Long)) && within(com.hnust.aopdemo..*) ")
    public void matchLongArg(){}

    @Pointcut("execution(public * com.hnust.aopdemo.service..*Service.*(..) throws java.lang.IllegalAccessException) && within(com.hnust.aopdemo..*)")
    public void matchException(){}

    @Pointcut("execution(java.util.List com.hnust.aopdemo..*.*(..)) && within(com.hnust.aopdemo..*)")
    public void matchReturn(){}


    /******advice********/
    /**
     * 结合before获取参数
     * @param productId
     */
    @Before("matchLongArg() && args(productId)")
    public void before(Long productId){
        System.out.println("###before,get args:"+productId);
    }


//    /**
//     * 比较强大，相对与before、after等的组合
//     * @param joinPoint
//     * @return
//     */
//    @Around("matchReturn()")
//    public java.lang.Object after(ProceedingJoinPoint joinPoint){
//        System.out.println("around ###before ");
//        java.lang.Object result = null;
//        try{
//            result = joinPoint.proceed(joinPoint.getArgs());
//            System.out.println(" around ###after returning");
//        }catch (Throwable e){
//            System.out.println("around ###ex");
//            //throw
//            e.printStackTrace();
//        }finally {
//            System.out.println("###finally");
//        }
//        return result;
//    }

//    @After("matchAnno()")
//    public void afterTest() {
//        System.out.println("============");
//        System.out.println("after is beeing");
//    }

//    /**
//     * @AfterReturning:获取返回值
//     * result：为被拦截方法的返回值
//     * 返回值类型必须得写全路径，如java.lang.Object
//     * @param result
//     */
//    @AfterReturning(value = "matchReturn()", returning = "result")
//    public void afterTest(java.util.List result) {
//        System.out.println("============");
//        System.out.println("afterreturning is beeing:" + result);
//    }

}
